package net.hn.hnms.biz.energy.service.impl;

import net.hn.hnms.biz.energy.mapper.WaterUseAnalysisMapper;
import net.hn.hnms.biz.energy.request.WaterUseAnalysisReq;
import net.hn.hnms.biz.energy.service.WaterUseAnalysisService;
import net.hn.hnms.biz.energy.vo.WaterUseAnalysisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @author 刘昊
 * @date 2024-03-01 9:39
 */
@Service
public class WaterUseAnalysisServiceImpl implements WaterUseAnalysisService {

    @Autowired
    WaterUseAnalysisMapper waterUseAnalysisMapper;

    @Override
    public List<WaterUseAnalysisVo> getYearAnalysis(WaterUseAnalysisReq waterUseAnalysisReq) {

        WaterUseAnalysisVo result = new WaterUseAnalysisVo();
        List<WaterUseAnalysisVo> waterAnalysisYearList = waterUseAnalysisMapper.getWaterAnalysisYear(waterUseAnalysisReq.getMineCode());
//        String yearWatter = map.get("year_on_year_change") == null ?"0":map.get("year_on_year_change").toString();
//        // todo 准确性代确认
//        BigDecimal yw = new BigDecimal(yearWatter).divide(new BigDecimal("100"),2, RoundingMode.CEILING);
//        result.setYearAnalysisWatter(yw);
        waterAnalysisYearList.forEach(
                item -> {
                    String yearWatter = item.getYearWatter() == null ?"0":item.getYearWatter().toString();
                    BigDecimal mw = new BigDecimal(yearWatter).divide(new BigDecimal("100"),2, RoundingMode.CEILING);
                    item.setYearWatter(mw);
                    if (waterUseAnalysisReq.getPreWarningValue() != null) {
                        item.setPreWarningLevel(item.getYearWatter().compareTo(waterUseAnalysisReq.getPreWarningValue()) <= 0);
                    }
                }
        );
        return waterAnalysisYearList;
    }

    @Override
    public List<WaterUseAnalysisVo> getMonthAnalysis(WaterUseAnalysisReq waterUseAnalysisReq) {

        WaterUseAnalysisVo result = new WaterUseAnalysisVo();
        List<WaterUseAnalysisVo> waterList = waterUseAnalysisMapper.getWaterAnalysisMonth(waterUseAnalysisReq.getMineCode());
//        String monthWatter = map.get("month_on_month_change") == null ?"0":map.get("month_on_month_change").toString();
//        // todo 准确性代确认
//        BigDecimal mw = new BigDecimal(monthWatter).divide(new BigDecimal("100"),2, RoundingMode.CEILING);
//        result.setYearAnalysisWatter(mw);
        waterList.forEach(
                item -> {
                    String monthWatter = item.getMonthWatter() == null ?"0":item.getMonthWatter().toString();
                    BigDecimal mw = new BigDecimal(monthWatter).divide(new BigDecimal("100"),2, RoundingMode.CEILING);
                    item.setMonthWatter(mw);
                    if (waterUseAnalysisReq.getPreWarningValue() != null) {
                        item.setPreWarningLevel(item.getMonthWatter().compareTo(waterUseAnalysisReq.getPreWarningValue()) <= 0);
                    }
                }
        );
        return waterList;
    }

    @Override
    public List<WaterUseAnalysisVo> getYearOnYearChange(WaterUseAnalysisReq waterUseAnalysisReq) {

        String mineCode = waterUseAnalysisReq.getMineCode() == null ? null : waterUseAnalysisReq.getMineCode();
        String companyCode = waterUseAnalysisReq.getCompanyCode() == null ? null : waterUseAnalysisReq.getCompanyCode();
        List<WaterUseAnalysisVo> waterList = waterUseAnalysisMapper.getYearOnYearChange(mineCode,companyCode);

        waterList.forEach(
                item -> {
                    String yearWatter = item.getYearWatter() == null ?"0":item.getYearWatter().toString();
                    BigDecimal mw = new BigDecimal(yearWatter).divide(new BigDecimal("100"),4, RoundingMode.CEILING);
                    item.setMonthWatter(mw);
                    if (waterUseAnalysisReq.getPreWarningValue() != null) {
                        item.setPreWarningLevel(item.getYearWatter().compareTo(waterUseAnalysisReq.getPreWarningValue()) <= 0);
                    }
                }
        );
        return waterList;
    }

    @Override
    public List<WaterUseAnalysisVo> getMonthOnMonthChange(WaterUseAnalysisReq waterUseAnalysisReq) {

        String mineCode = waterUseAnalysisReq.getMineCode() == null ? null : waterUseAnalysisReq.getMineCode();
        String companyCode = waterUseAnalysisReq.getCompanyCode() == null ? null : waterUseAnalysisReq.getCompanyCode();
        List<WaterUseAnalysisVo> waterList = waterUseAnalysisMapper.getMonthOnMonthChange(mineCode,companyCode);

        waterList.forEach(
                item -> {
                    String monthWatter = item.getMonthWatter() == null ?"0":item.getMonthWatter().toString();
                    BigDecimal mw = new BigDecimal(monthWatter).divide(new BigDecimal("100"),4, RoundingMode.CEILING);
                    item.setMonthWatter(mw);
                    if (waterUseAnalysisReq.getPreWarningValue() != null) {
                        item.setPreWarningLevel(item.getMonthWatter().compareTo(waterUseAnalysisReq.getPreWarningValue()) <= 0);
                    }
                }
        );
        return waterList;
    }
}
